<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic\photo;

use OSS\OssClient;
use OSS\Core\OssException;
use think\Db;

class PhotoPayLogic
{

    /**
     * User: Simony
     * Function: 查看照片是否过期
     * @param $member_id
     * @param $file_id
     * @param int $type 查看类型 2：相片、3：阅后即焚
     * @return bool
     */
    public function viewPhotoIsExpire($member_id, $file_id, $type = 2)
    {
        $fileModel = model('common/member/File');
        $file = $fileModel->getInfoByID($file_id);

        $view_member_id = $file['member_id'];
        $photoPayModel = model('common/photo/Pay');
        $where = [
            ['type', 'eq', $type],
            ['member_id', 'eq', $member_id],
            ['file_id', 'eq', $file_id],
            ['view_member_id', 'eq', $view_member_id],
        ];

        $photoPay = $photoPayModel->getInfo($where);
        if (empty($photoPay)) return true; //未付费过

        if ($photoPay['expire_time'] == 0 || $photoPay['expire_time'] > time()) return false; //永不过期 or 未过期
        return true;
    }

    /**
     * User: Simony
     * Function: 付费解锁
     * @param $member_id 解锁用户
     * @param $file_id 解锁相册传入对方会员id  解锁照片传图片文件id
     * @param $type  解锁类型  1：相册、2：相片
     * @return array
     */
    public function payUnlock($member_id, $file_id, $type)
    {

        switch ($type) {
            case 1:
                $been_member_id = $file_id;
                $res = $this->unlockAlbum($member_id, $been_member_id);
                break;
            case 2:
                $res = $this->unlockPhoto($member_id, $file_id);
                break;
            default:
                break;
        }
        return ds_arr($res['code'], $res['msg'], $res['result']);

    }


    /***
     * User: Simony
     * Function:解锁相册
     * @param $member_id 解锁用户
     * @param $file_id 被解锁用户
     */
    public function unlockAlbum($member_id, $been_member_id)
    {
        $file_id = $been_member_id;
        $memberModel = model('common/member/Member');
        $member = $memberModel->getInfoByID($member_id);
        if (empty($member)) return ds_arr(ERROR, '会员不存在');

        $been_member = $memberModel->getInfoByID($been_member_id);
        if (empty($been_member)) return ds_arr(ERROR, '被解锁用户不存在');
        if ($member_id == $been_member_id) return ds_arr(ERROR, '自己查看自己无需付费');
        if ($been_member['album_auth'] != 1) return ds_arr(ERROR, '该用户已开发相册权限，无需付费解锁');
        $been_member_id = $been_member['id'];

        $authLogic = model('common/AuthLogic', 'logic');
        $levelModel = model('common/member/Level');
        $viewLogic = model('common/view/ViewLogLogic', 'logic');

        //添加查看日志
        $is_expire = $authLogic->isExpire($member_id, $been_member_id, 3); //1，会员信息 2微信+聊天社交 3，相册
        if ($is_expire == false) return ds_arr(ERROR, '该会员相册还在有效时间内无需付费，可直接查看');

        $surplus_num = $authLogic->getDaySurplusViewNum($member_id, $levelModel::DAY_VIEW_MEMBER_ALBUM_NUM);
        if ($surplus_num > 0) return ds_arr(ERROR, '该会员相册还有次数查看，无需付费');

        //余额不足
        $price = $been_member['album_price'] > 0 ? $been_member['album_price'] : 0;
        if (floatval($member['available_balance']) < floatval($price)) return ds_arr(JUMP_RECHARGE, '余额不足，还差' . ($price - $member['available_balance']) . '金币,请及时充值');

        //添加查看日志

        $res = $viewLogic->addViewLog($member_id, $been_member_id, $levelModel::DAY_VIEW_MEMBER_ALBUM_EFFECTIVE_TIME);
        if ($res['code'] != SUCCESS) return ds_arr($res['code'], $res['msg'], $res['result']);

        $current_time = time();
        //添加收费相册
        $photoPayModel = model('common/photo/Pay');
        $add_photo_pay_data = [
            'type' => 1,  //1：相册、2：相片、3：阅后即焚  这个位置时不能修改的
            'file_id' => $file_id,
            'member_id' => $member_id,
            'view_member_id' => $been_member_id,
            'payment_amount' => $price,
            'pay_time' => $current_time,
        ];
        $config_day = $authLogic->getExpireConfig($member_id, $levelModel::DAY_VIEW_MEMBER_ALBUM_EFFECTIVE_TIME);
        $add_photo_pay_data['expire_time'] = $current_time + $config_day * 86400;

        $add_photo_pay_res = $photoPayModel->add($add_photo_pay_data);
        if (!$add_photo_pay_res) return ds_arr(ERROR, '收费相册添加失败');

        //变更付费用户余额
        //余额变更记录
        $desc = '付费相册';
        $update_available_balance_res = model('common/member/MemberLogic', 'logic')->updateAvailableBalance($member_id, -$price, 0, 0, $desc);
        if ($update_available_balance_res['code'] != SUCCESS) return ds_arr($update_available_balance_res['code'], $update_available_balance_res['msg']);

        //变更收费用户的余额
        $desc = $member['nickname'] . '解锁了我的相册,获得' . $price . '金币';
        $update_available_balance_res = model('common/member/MemberLogic', 'logic')->updateAvailableBalance($been_member_id, $price, 0, 4, $desc,$member_id);
        if ($update_available_balance_res['code'] != SUCCESS) return ds_arr($update_available_balance_res['code'], $update_available_balance_res['msg']);

        return ds_arr(SUCCESS, '相册付费成功');
    }

    /***
     * User: Simony
     * Function: 解锁照片
     * @param $member_id 解锁者
     * @param $file_id
     */
    public function unlockPhoto($member_id, $file_id)
    {

        $memberModel = model('common/member/Member');
        $member = $memberModel->getInfoByID($member_id);
        if (empty($member)) return ds_arr(ERROR, '会员不存在');

        $fileModel = model('common/member/File');
        $file = $fileModel->getInfoByID($file_id);

        if (empty($file)) return ds_arr(ERROR, '解锁的照片不存在');
        if ($file['type'] != 3) return ds_arr(ERROR, '非红包照片无需解锁'); //file 文件类型 1：照片、2：阅后即焚、3：红包照片

        $been_member = $memberModel->getInfoByID($file['member_id']);
        if (empty($been_member)) return ds_arr(ERROR, '文件所属者不存在');
        $been_member_id = $been_member['id'];

        $viewPhotoIsExpire = $this->viewPhotoIsExpire($member_id, $file_id, 2); //photo_pay 查看类型 1：相册、2：相片、3：阅后即焚',

        if ($viewPhotoIsExpire == false) return ds_arr(ERROR, '该会员照片还在有效时间内无需付费，可直接查看');

        //余额不足
        $price = $file['red_packet_price'] > 0 ? $file['red_packet_price'] : 0;
        if (floatval($member['available_balance']) < floatval($price)) return ds_arr(JUMP_RECHARGE, '余额不足，还差' . ($price - $member['available_balance']) . '金币,请及时充值');


        $current_time = time();
        //添加收费相册
        $photoPayModel = model('common/photo/Pay');
        $add_photo_pay_data = [
            'type' => 2,  //1：相册、2：相片、3：阅后即焚  这个位置时不能修改的
            'file_id' => $file_id,
            'member_id' => $member_id,
            'view_member_id' => $been_member_id,
            'payment_amount' => $price,
            'pay_time' => $current_time,
            'expire_time' => 0
        ];

        $add_photo_pay_res = $photoPayModel->add($add_photo_pay_data);
        if (!$add_photo_pay_res) return ds_arr(ERROR, '收费照片添加失败');


        //变更付费用户余额
        //余额变更记录
        $desc = '红包照片';
        $update_available_balance_res = model('common/member/MemberLogic', 'logic')->updateAvailableBalance($member_id, -$price, 0, 0, $desc);
        if ($update_available_balance_res['code'] != SUCCESS) return ds_arr($update_available_balance_res['code'], $update_available_balance_res['msg']);

        //变更收费用户的余额
        $desc = $member['nickname'] . '查看了我的红包照片,获得' . $price . '金币';
        $update_available_balance_res = model('common/member/MemberLogic', 'logic')->updateAvailableBalance($been_member_id, $price, 0, 4, $desc, $member_id);
        if ($update_available_balance_res['code'] != SUCCESS) return ds_arr($update_available_balance_res['code'], $update_available_balance_res['msg']);

        return ds_arr(SUCCESS, '照片付费成功');
    }


}
